// 空间复杂度为1，不创建新的字符串

function reverse(str, start = 0, end = str.length) {
    let left = start;
    let right = end - 1;
    while (left < right) {
      let tmp = str[left];
      str[left] = str[right];
      str[right] = tmp;
      left++;
      right--;
    }
  }
  var reverseStr = function (str, k) {
      let s = Array.from(str);
      for(let i = 0;i < s.length;i+=2*k) {
          if(i+k<=s.length) {
              reverse(s,i,i+k);
              continue;
          }
         
          if(i+k>s.length) {
              reverse(s,i);
          }
      }
      return s.join("");
  };
  // reverseStr("abcdefg",8);